<template>
    <el-card class="layout-card">
        <STable ref="tableRef" :fetch-list="fetchList" :search-rules="searchRules" :search-form="searchForm"
            :border="false" highlight-current-row @current-change="handleCurrentChange">
            <template #form>
                <el-col :span="6">
                    <el-form-item label="查询条件1">
                        <el-input v-model="searchForm.test_purpose" placeholder="测试目的" />
                    </el-form-item>
                </el-col>

                <el-col :span="6">
                    <el-form-item label="查询条件2">
                        <el-input v-model="searchForm.test_sheet_number" placeholder="测试单编号" />
                    </el-form-item>
                </el-col>

                <el-col :span="6">
                    <el-form-item label="查询条件3">
                        <el-input v-model="searchForm.test_sheet_number" placeholder="测试单编号" />
                    </el-form-item>
                </el-col>
            </template>

            <template #action>
                <el-button type="primary" :icon="Pointer" @click="handleCheck()">
                    开始检测
                </el-button>
            </template>

            <template #table>
                <el-table-column prop="id" label="ID" width="70" />
                <el-table-column prop="test_purpose" label="测试目的" width="300" show-overflow-tooltip />
                <el-table-column prop="test_sheet_number" label="测试单编号" width="250" show-overflow-tooltip />
                <el-table-column prop="applicant" label="申请人" width="100" show-overflow-tooltip />
                <el-table-column prop="applicant_department" label="申请人部门" width="100">
                    <template #default="{ row }">
                        {{ applicantDepartment[row.applicant_department] }}
                    </template>
                </el-table-column>
                <el-table-column prop="production_batch_number" label="生产批次号" width="100" show-overflow-tooltip />
                <el-table-column prop="cell_model" label="电芯型号" width="100" show-overflow-tooltip />
                <el-table-column prop="sample_recipient" label="收样人" width="100">
                    <template #default="{ row }">
                        {{ sampleRecipient[row.sample_recipient] }}
                    </template>
                </el-table-column>
                <el-table-column prop="sample_recipient_time" label="收样时间" width="200" />
                <el-table-column prop="is_save_sample" label="保留样品" width="100">
                    <template #default="{ row }">
                        <el-tag :type="row.is_save_sample == 1 ? 'success' : 'danger'" size="large">
                            {{ YerOrNo[row.is_save_sample] }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="is_emergent_test" label="为紧急测试" width="100">
                    <template #default="{ row }">
                        <el-tag :type="row.is_emergent_test == 1 ? 'success' : 'danger'" size="large">
                            {{ YerOrNo[row.is_emergent_test] }}
                        </el-tag>
                    </template>
                </el-table-column>

                <el-table-column prop="manager_review" label="部门经理审核" width="120" />
                <el-table-column prop="tc_review" label="测试中心审核" width="120" />

                <el-table-column fixed="right" width="180" label="操作">
                    <template #default="{ row }">
                        <!-- <el-button type="primary" link @click="router.push('/sample/edit/' + row.id)" :icon="Edit">
                修改
              </el-button>
              <el-button type="danger" link @click="handleDelete(row.id)" :icon="Delete" v-if="permissionStore.permissions.includes('样品删除')">
                删除
              </el-button> -->
                    </template>
                </el-table-column>
            </template>
        </STable>
    </el-card>
</template>

<script setup lang='ts'>
import { ref, inject } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Plus, Edit, Delete, Pointer } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox, ElTable, ElLoading } from 'element-plus'
import sampleApi from '@/api/sample'
import STable from '@/components/Search.vue'
import axios from 'axios'
import { sampleRecipient, applicantDepartment, YerOrNo } from '@/utils/options'
import { getToken } from '@/utils/cookies'
import { withLoading } from '@/utils/laoding'
import { usePermissionStore } from '@/store/modules/permission'

const reload = inject<Function>('reload')

const router = useRouter()
const route = useRoute()
const permissionStore = usePermissionStore()


const searchForm = ref({
    test_purpose: '',
    test_sheet_number: '',
    applicant_department: ''
})

const fetchList = function (params: any) {
    return sampleApi.index(params)
}
const handleDelete = function (id: number) {
    return ElMessageBox.confirm(
        '是否确认删除?',
        '警告',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        sampleApi.delete(id).then(() => {
            ElMessage({
                type: 'success',
                message: '删除成功',
            })
            reload && reload()
        }).catch(() => {

        })
    })
        .catch(() => {

        })
}
const searchRules = ref({
    // name: [{ max: 10, message: '最大长度是 10 个字符', trigger: 'blur' }]
})

const tableRef = ref<InstanceType<typeof ElTable>>()
const currentRow = ref()

const handleCheck = () => {
    const loading = ElLoading.service({
        lock: true,
        text: 'Loading',
        background: 'rgba(0, 0, 0, 0.7)',
    })

    try {
        axios({
            method: 'post',
            headers: {
                'Content-Type': 'application/json; charset=utf-8'
            },
            url: `http://10.127.176.180:5000/prediction`,

        }).then((res) => {
            console.log(res)
            ElMessage({
                type: 'success',
                message: '操作成功，请耐心等候!',
            })
        })
    } catch (error) {
        console.log(error);
    }

    loading.close()

}
const handleCurrentChange = (val: any) => {
    currentRow.value = val
}
const downloadFile = (data: any, fileName: string) => {
    const blob = new Blob([data]);
    const downloadElement = document.createElement('a');
    const href = window.URL.createObjectURL(blob);
    downloadElement.href = href;
    downloadElement.download = fileName;
    document.body.appendChild(downloadElement);
    downloadElement.click();
    document.body.removeChild(downloadElement);
    window.URL.revokeObjectURL(href);
}

</script>